#!/usr/bin/python
# coding=utf8
#
# Copyright (C) 2011 XNData contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = "zhangchitc@gmail.com"


import cookielib
import urllib2, urllib
import xndata.client
from xndata.frontend.comment import CommentPageFetcher
from xndata.frontend.comment import CommentPageParser


class XNCommentClient (xndata.client.XNClient):

    def __init__ (self, xnclient=None, uname=None, upass=None):
        if xnclient is not None:
            # initialize with already-logined xnclient
            self.opener = xnclient.opener
            self.user_id = xnclient.user_id
            self.user_name = xnclient.user_name

        if (uname is not None) and (upass is not None):
            # initialize with username and password
            xndata.client.XNClient.__init__ (self, uname, upass)


    def getTotalPhotoComment (self, user_id, photo_id):
        html = CommentPageFetcher.fetch_photo_comment_list_page (
                self.opener, user_id, photo_id, 0)
        return CommentPageParser.get_total_comment (html)

    def getTotalPhotoCommentList (self, user_id, photo_id):
        return (self.getTotalPhotoComment (user_id, photo_id) - 1) / 30 + 1

    def getNextPhotoCommentList (self, user_id, photo_id, cur_page = 0):
        html = CommentPageFetcher.fetch_photo_comment_list_page (
                self.opener, user_id, photo_id, cur_page)
        next_comment_list = CommentPageParser.get_comment_list (html, cur_page)
        return next_comment_list

    def getAllPhotoComment (self, user_id, photo_id):
        total_comment_list = self.getTotalPhotoCommentList (user_id, photo_id)
        all_comment_list = self.getNextPhotoCommentList (user_id, photo_id)
        for i in range (1, total_comment_list):
            tmp_comment_list = self.getNextPhotoCommentList (user_id, photo_id, i)
            tmp_comment_list.clist.extend (all_comment_list.clist)
            all_comment_list = tmp_comment_list
        return all_comment_list


    def getTotalAlbumComment (self, user_id, album_id):
        html = CommentPageFetcher.fetch_album_comment_list_page (
                self.opener, user_id, album_id, 0)
        return CommentPageParser.get_total_comment (html)

    def getTotalAlbumCommentList (self, user_id, album_id):
        return (self.getTotalAlbumComment (user_id, album_id) - 1) / 30 + 1

    def getNextAlbumCommentList (self, user_id, album_id, cur_page = 0):
        html = CommentPageFetcher.fetch_album_comment_list_page (
                self.opener, user_id, album_id, cur_page)
        next_comment_list = CommentPageParser.get_comment_list (html, cur_page)
        return next_comment_list

    def getAllAlbumComment (self, user_id, album_id):
        total_comment_list = self.getTotalAlbumCommentList (user_id, album_id)
        all_comment_list = self.getNextAlbumCommentList (user_id, album_id)
        for i in range (1, total_comment_list):
            tmp_comment_list = self.getNextAlbumCommentList (user_id, album_id, i)
            tmp_comment_list.clist.extend (all_comment_list.clist)
            all_comment_list = tmp_comment_list
        return all_comment_list
    
    
    def getTotalNoteComment (self, user_id, note_id):
        html = CommentPageFetcher.fetch_note_comment_list_page (
                self.opener, user_id, note_id, 0)
        return CommentPageParser.get_total_comment (html)

    def getTotalNoteCommentList (self, user_id, note_id):
        return (self.getTotalNoteComment (user_id, note_id) - 1) / 30 + 1

    def getNextNoteCommentList (self, user_id, note_id, cur_page = 0):
        html = CommentPageFetcher.fetch_note_comment_list_page (
                self.opener, user_id, note_id, cur_page)
        next_comment_list = CommentPageParser.get_comment_list (html, cur_page)
        next_comment_list.clist.reverse ()
        return next_comment_list

    def getAllNoteComment (self, user_id, note_id):
        total_comment_list = self.getTotalNoteCommentList (user_id, note_id)
        all_comment_list = self.getNextNoteCommentList (user_id, note_id)
        for i in range (1, total_comment_list):
            tmp_comment_list = self.getNextNoteCommentList (user_id, note_id, i)
            tmp_comment_list.clist.extend (all_comment_list.clist)
            all_comment_list = tmp_comment_list
        return all_comment_list


    def getAllStatusComment (self, user_id, status_id):
        html = CommentPageFetcher.fetch_status_comment_list_page (
                self.opener, user_id, status_id)
        return CommentPageParser.get_status_comment_list (html)

if __name__ == "__main__":
    client = XNStatusClient (uname="zhangchitc@gmail.com", upass="aaaaaaaa")
    
    print client.getTotalStatus ("247780420")
    
    # Other's albums
    status_list = client.getNextStatusList (user_id="247780420")
    #print status_list
    status_list = client.getNextStatusList (prev_status_list=status_list)
    print status_list
   # friend_list2 = client.getNextFriendList (prev_friend_list=friend_list1)
    #print friend_list2


    status_list = client.getAllStatus (user_id="242124580")
    print status_list

    # other's album
    #photo_list = client.getNextPhotoList (user_id="247780420", album_id="282652960")
    #print photo_list
    # my album
    #photo_list = client.getNextPhotoList (user_id="242124580", album_id="432244996")
    #print photo_list
    #print client.getAllPhoto ("242124580", "432244996")
    #print client.getAllPhoto ("247780420", "282652960")
    # print client.getAllPhoto ("252475509", "265669703")
    #print client.getAllPhoto ("242124580", "272296924")
